<?php
require_once ("Channel.class.php");

//请开发者设置自己的apiKey与secretKey
$apiKey = "GdnAo87G7O4rPQhIWnyrzFyA";
$secretKey = "Rck7RMCkGAQRAXuRXMaRBRbjxQONtGdM";

function error_output($str) {
	echo "\033[1;40;31m" . $str . "\033[0m" . "\n";
}

function right_output($str) {
	echo "\033[1;40;32m" . $str . "\033[0m" . "\n";
}

//推送android设备消息
function test_pushMessage_android() {
	global $apiKey;
	global $secretKey;
	$channel = new Channel($apiKey, $secretKey);

	//推送消息到某个user，设置push_type = 1; 
	//推送消息到一个tag中的全部user，设置push_type = 2;
	//推送消息到该app中的全部user，设置push_type = 3;
	$push_type = (int) $_REQUEST['push_type'];
	$title = $_REQUEST['title'];
	$desc = $_REQUEST['desc'];
	$tag_name = $_REQUEST['tag_name'];
	$user_id = $_REQUEST['user_id'];
	$url = $_REQUEST['url'];

	if ($push_type === 1) {
		$optional[Channel :: USER_ID] = $user_id;
	} else
		if ($push_type === 2) {
			$optional[Channel :: TAG_NAME] = $tag_name;
		}

	if ($url === null || strlen($url) === 0) {
		$url = "http://www.oldfeel.cn";
	}
	//指定发到android设备
	$optional[Channel :: DEVICE_TYPE] = 3;
	//指定消息类型为通知
	$optional[Channel :: MESSAGE_TYPE] = 1;

	$arr = array (
		'title' => $_REQUEST['title'],
		'description' => $_REQUEST['desc'],
		'notification_basic_style' => 7,
		'open_type' => 1,
		'url' => $url
	);
	$message = json_encode($arr);

	$message_key = "msg_key";
	$ret = $channel->pushMessage($push_type, $message, $message_key, $optional);
	if (false === $ret) {
		error_output('WRONG, ' . __FUNCTION__ . ' ERROR!!!!!');
		error_output('ERROR NUMBER: ' . $channel->errno());
		error_output('ERROR MESSAGE: ' . $channel->errmsg());
		error_output('REQUEST ID: ' . $channel->getRequestId());
	} else {
		right_output('推送成功, ' . __FUNCTION__ . ' OK!!!!!');
		right_output('result: ' . print_r($ret, true));
	}
}

function test_setTag($tag_name, $user_id) {
	global $apiKey;
	global $secretKey;
	$channel = new Channel($apiKey, $secretKey);
	$optional[Channel :: USER_ID] = $user_id;
	$ret = $channel->setTag($tag_name, $optional);
	if (false === $ret) {
		error_output('WRONG, ' . __FUNCTION__ . ' ERROR!!!!!');
		error_output('ERROR NUMBER: ' . $channel->errno());
		error_output('ERROR MESSAGE: ' . $channel->errmsg());
		error_output('REQUEST ID: ' . $channel->getRequestId());
		return false;
	} else {
		right_output('SUCC, ' . __FUNCTION__ . ' OK!!!!!');
		right_output('result: ' . print_r($ret, true));
		return $ret['response_params']['tid'];
	}
}

function test_fetchTag($tag_name = null) {
	global $apiKey;
	global $secretKey;
	$channel = new Channel($apiKey, $secretKey);
	$optional[Channel :: TAG_NAME] = $tag_name;
	$ret = $channel->fetchTag($optional);
	if (false === $ret) {
		error_output('WRONG, ' . __FUNCTION__ . ' ERROR!!!!!');
		error_output('ERROR NUMBER: ' . $channel->errno());
		error_output('ERROR MESSAGE: ' . $channel->errmsg());
		error_output('REQUEST ID: ' . $channel->getRequestId());
	} else {
		right_output('SUCC, ' . __FUNCTION__ . ' OK!!!!!');
		right_output('result: ' . print_r($ret, true));
	}

}

$push_type = $_REQUEST['push_type'];
if ($push_type <= 0) {
	test_setTag($_REQUEST['tag_name'], $_REQUEST['user_id']);
} else {
	test_pushMessage_android();
}